<?php
/**
 * Created by PhpStorm.
 * User: qinlu
 * Date: 2018/7/25
 * Time: 下午3:23
 */

namespace Admin\Controller;
use Think\Page;

class CertificationController extends BaseController
{
    /**
     * 待审核列表
     */
    public function examine(){
        $username = I('username');
        $map = array();
        if($username){
            $map['username'] = array('like',"%$username%");
        }
        $map['isvalid'] = "1";
        $map['status'] = ['lt',2];
        $count = M('sm')->where($map)->count();
        $page = new Page($count);
        $lists  = M('sm')->where($map)->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('page',$page->show());
        $this->assign('lists',$lists);
        $this->display('examine_list');
    }


    /**
     * 成功认证列表
     */
    public function lists(){
        $username = I('username');
        $map = array();
        if($username){
            $map['username'] = array('like',"%$username%");
        }
        $map['status'] = "2";
        $map['isvalid'] = "1";
        $count = M('sm')->where($map)->count();
        $page = new Page($count);
        $lists  = M('sm')->where($map)->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('page',$page->show());
        $this->assign('lists',$lists);
        $this->display();
    }


    /**
     * 修改实名认证状态
     */
    public function update_certification_status(){
        $status = I('status');
        $id = I('id');
        $where['sm_id'] = $id;
        $data['status'] = $status;

        $log = M('sm')->where($where)->find();

        if( !$log )
        {
            return $this->error("无此记录");
        }

        $res=M('sm')->where($where)->save($data);
        
        if($res){
            if( $status )
            {
                if($status == 2)
                {
                    $content = "恭喜你，实名认证已通过！";
                }else{
                    $content = "对不起，你的实名认证资料无法通过审核";
                }
                \Common\Logic\MessageLogic::sendUserMsg([
                    "user_id" => $log['user_id'],
                    "type" => \Common\Logic\MessageLogic::SYSTEM_MSG_SIGLE,
                    "title" => "实名认证通知",
                    "content" => $content,
                    "ext" => [
                        "log_id" => $log['id'],
                    ],
                    "notice" => true
                ]);
            }
            $this->Certification_success('修改成功');
        }else {
            $this->Certification_error('修改失败');
        }
    }


    /**
     * 删除
     */
    public function Certification_del(){
        $id = I('id');
        $where['sm_id'] = $id;
        // $data['isvalid'] = 0;
        $res=M('sm')->where($where)->delete();
        if($res){
            $this->Certification_success('删除成功');
        }else {
            $this->Certification_error('删除失败');
        }
    }

    /**
     * 批量删
     */
    public function right_del(){
        $id = I('del_id');
        if(is_array($id)){
            $id = implode(',', $id);
        }
        if(!empty($id)){
            $r = M('sm')->where("sm_id in ($id)")->delete();
            if($r){
                respose(1);
            }else{
                respose('删除失败');
            }
        }else{
            respose('参数有误');
        }
    }


    //返回错误信息
    public function Certification_error($message,$data=''){
        $error=[
            'message'=>$message,
            'result'=>'400',
            'data'=>$data
        ];
        exit(json_encode($error));
    }
    //返回成功
    public function Certification_success($message,$data=''){
        $success=[
            'message'=>$message,
            'result'=>'200',
            'data'=>$data
        ];
        exit(json_encode($success));
    }

}